#include "apriori.h"

Apriori::Apriori()
{
    minSupport = 0.20;
    minConfidence = 0.80;
}

void Apriori::setMinSupport(double minSupport)
{
    this->minSupport = minSupport;
}

void Apriori::setMinConfidence(double minConfidence)
{
    this->minConfidence = minConfidence;
}

void Apriori::setSessions(QList<Session *> *sessions)
{
    this->sessions = sessions;
}

double Apriori::support(QPair<QStringList, QStringList> pages)
{
    // In QPair, first gonna be A, and secound gonna be B

    double supp = 0;
    Session *currentSession = 0;

    // Find all elements from set A and B in all sessions
    int counter = 0;

    // Join A and B to one QStringList
    QStringList pagesUnion;
    pagesUnion.append(pages.first);
    pagesUnion.append(pages.second);

    for(int i = 0; i < sessions->size(); ++i)
    {
        bool sessionHasAllPages = true;
        currentSession = sessions->at(i);

        for(int j = 0; j < pagesUnion.size(); ++j)
        {
            if(currentSession->getPages().indexOf(pagesUnion.at(j)) == -1)
            {
                // One of page does not exists in session
                sessionHasAllPages = false;
            }
        }

        if(sessionHasAllPages == true)
            counter++;
    }

    supp = (double)counter / (double)sessions->size();
    qDebug() << "counter" << counter;
    return supp;
}

double Apriori::support(QStringList pages)
{
    double supp = 0;
    Session *currentSession = 0;

    // Find all elements from given pages
    int counter = 0;

    for(int i = 0; i < sessions->size(); ++i)
    {
        bool sessionHasAllPages = true;
        currentSession = sessions->at(i);

        for(int j = 0; j < pages.size(); ++j)
        {
            if(currentSession->getPages().indexOf(pages.at(j)) == -1)
            {
                // One of page does not exists in session
                sessionHasAllPages = false;
            }
        }

        if(sessionHasAllPages == true)
            counter++;
    }

    supp = (double)counter / (double)sessions->size();
    //qDebug() << "counter" << counter;
    return supp;
}

void Apriori::findAllCandidats()
{
    int width = 0;
    Session *currentSession = 0;

    // Find the widest session
    for(int i = 0; i < sessions->size(); ++i)
    {
        currentSession = sessions->at(i);
        if(currentSession->getPages().size() > width)
            width = currentSession->getPages().size();
    }

    qDebug() << width << "wide";

    for(int w = 1; w <= width; ++w)
    {
        for(int i = 0; i < sessions->size(); ++i)
        {
            currentSession = sessions->at(i);

            // Make sure that current session has enough pages inside
            if(currentSession->getPages().size() >= w)
            {
                // Copy w pages from session
                QStringList subSetPages;
                for(int j = 0; j < w; ++j)
                    subSetPages.append(currentSession->getPages().at(j));

                //qDebug() << w << subSetPages;
                if(support(subSetPages) > minSupport)
                    qDebug() << support(subSetPages) << subSetPages;
            }
        }
    }
}

void Apriori::test()
{
    qDebug() << "Test of Apriori::test()";

    // QStringList
    //QStringList currentPages = sessions->at(1)->getPages();
    ///shuttle/countdown/", "/facilities/lcc.html", "/ksc.html"
    //("/shuttle/countdown/", "/facilities/lcc.html", "/ksc.html")
    //QStringList A;
    //A.append("/");
    //A.append("/");
    //QStringList B;
    //B.append("/shuttle/countdown/");
    //B.append("/shuttle/missions/sts-71/mission-sts-71.html");

    //QPair<QStringList, QStringList> p;
    //p.first = A;
    //p.second = B;

    QStringList p;
    //p.append("/");
    p.append("/shuttle/countdown/");
    p.append("/shuttle/missions/sts-71/images/images.html");

    //qDebug() << support(p) << sessions->size();
    //qDebug() << support(p) << sessions->size();

    findAllCandidats();

    //qDebug() << p;
}
